package com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.repository.HddcWyFaultsvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.repository.HddcWyFaultsvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.repository.entity.HddcWyFaultsvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.service.HddcWyFaultsvypointService;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.viewobjects.HddcWyFaultsvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyFaultSvyPoint.viewobjects.HddcWyFaultsvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Service
public class HddcWyFaultsvypointServiceImpl implements HddcWyFaultsvypointService {

	@Autowired
    private HddcWyFaultsvypointRepository hddcWyFaultsvypointRepository;
    @Autowired
    private HddcWyFaultsvypointNativeRepository hddcWyFaultsvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyFaultsvypoints(HddcWyFaultsvypointQueryParams queryParams , int curPage,
                                                int pageSize) {
        Page<HddcWyFaultsvypointEntity> hddcWyFaultsvypointPage =
                this.hddcWyFaultsvypointNativeRepository.queryHddcWyFaultsvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyFaultsvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyFaultsvypointEntity getHddcWyFaultsvypoint(String id) {
        HddcWyFaultsvypointEntity hddcWyFaultsvypoint = this.hddcWyFaultsvypointRepository.findById(id).orElse(null);
         return hddcWyFaultsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyFaultsvypointEntity saveHddcWyFaultsvypoint(HddcWyFaultsvypointEntity hddcWyFaultsvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyFaultsvypoint.setId(uuid);
        hddcWyFaultsvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyFaultsvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyFaultsvypointRepository.save(hddcWyFaultsvypoint);
        return hddcWyFaultsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyFaultsvypointEntity updateHddcWyFaultsvypoint(HddcWyFaultsvypointEntity hddcWyFaultsvypoint) {
        hddcWyFaultsvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyFaultsvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyFaultsvypointRepository.save(hddcWyFaultsvypoint);
        return hddcWyFaultsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyFaultsvypoints(List<String> ids) {
        List<HddcWyFaultsvypointEntity> hddcWyFaultsvypointList = this.hddcWyFaultsvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyFaultsvypointList) && hddcWyFaultsvypointList.size() > 0) {
            for(HddcWyFaultsvypointEntity hddcWyFaultsvypoint : hddcWyFaultsvypointList) {
                this.hddcWyFaultsvypointRepository.delete(hddcWyFaultsvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyFaultsvypoint(HddcWyFaultsvypointQueryParams queryParams) {
        BigInteger bigInteger = hddcWyFaultsvypointNativeRepository.queryHddcWyFaultsvypoint(queryParams);
        return bigInteger;
    }

}
